
import LanguageMngTs from "../Managers/LanguageMngTs";
import LanguageMng from "../Managers/LanguageMng";
import GameDataUtilTs from "./GameDataUtilTs";
import LogicConfigTs from "./LogicConfigTs";

cc.Class({


    //初始化一个头像
    initHead:function (faceUrl,sprite) {
        let prefixIdx = faceUrl.indexOf("face_")
        if(prefixIdx > -1){
            let startIdx = prefixIdx + 5
            let endIdx = faceUrl.indexOf(".png")
            let faceNum = faceUrl.substr(startIdx,endIdx-startIdx)
            faceNum = parseInt(faceNum)

            if(faceNum > 0 && faceNum <= 15){
                let faceUrl = "faces/user_config_face"+faceNum
                sprite.wantUrl = faceUrl
                cc.loader.loadRes("faces/user_config_face"+faceNum,cc.SpriteFrame,function (err, frame) {
                    if(err || !frame){
                        console.error("Load default face failed! ",faceNum)
                        cc.gg.logicUtil._doSetSpriteFrame(sprite,null)
                    }else {
                        cc.gg.logicUtil._doSetSpriteFrame(sprite,frame,faceUrl)
                    }
                }.bind(this))
            }else {
                let faceUrl = "faces/user_config_face"+faceNum
                sprite.wantUrl = faceUrl
                cc.loader.loadRes(faceUrl,function (err, texture) {
                    if(err || !texture){
                        console.error("Load face image failed! ",faceNum)
                        cc.gg.logicUtil._doSetSpriteFrame(sprite,null)
                    }else {
                        cc.gg.logicUtil._doSetSpriteFrame(sprite,new cc.SpriteFrame(texture),faceUrl)
                    }
                }.bind(this))
            }

            return faceNum
        }else {
            // faceUrl = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3454158483,394355633&fm=15&gp=0.jpg"
            console.log("读取自定义图片：",faceUrl)
            sprite.wantUrl = faceUrl
            cc.loader.load(faceUrl,function (err, texurre) {
                if(err){
                    console.error(err)
                    return
                }

                if(!cc.isValid(sprite.node)){
                    console.warn("想要改变frame的节点已经不存在！")
                    return
                }

                if(!texurre){
                    console.error("Load face image failed! ",faceUrl)
                    cc.gg.logicUtil._doSetSpriteFrame(sprite,null)
                    return
                }

                let frame = new cc.SpriteFrame(texurre)
                cc.gg.logicUtil._doSetSpriteFrame(sprite,frame,faceUrl)
            })
        }
    },

    _doSetSpriteFrame(sprite,frame,frameUrl){
        if(!frame){
            sprite.wantUrl = null
            sprite.spriteFrame = null
            return
        }

        let wantUrl = sprite.wantUrl
        if(frameUrl === wantUrl){
            sprite.spriteFrame = frame
        }else {
            sprite.spriteFrame = null
        }
    },

    canActivityCollect:function (ts) {
        var timestamp = (new Date()).getTime()/1000;
        let nowT = Math.floor(timestamp / 86400)
        let lastT = Math.floor(ts / 86400)
        let canCollect = nowT !== lastT;
        cc.log("lastT:",lastT)
        cc.log("nowT:",nowT)
        return canCollect
    },

    async collectActivity (activityId) {
        let result = await cc.gg.httpUtil.httpPost("/activities/collect",{activityId:activityId})
        if (!result.succ) {
            cc.error(result.msg)
            return false
        }

        return true
    },

    async uploadFaceImgUrl (faceImgUrl){
        return await cc.gg.httpUtil.httpPost("/user/profile/img/upload",{img:faceImgUrl})
    },

    windowEnterFromTop:function (node) {
        node.stopAllActions()
        node.active = true
        node.position = node.parent.convertToNodeSpaceAR(cc.find("Canvas").convertToWorldSpaceAR(cc.v2(0,cc.winSize.height/2 + node.height)))
        node.runAction(cc.moveBy(0.12,0,-cc.winSize.height/2-node.height/2))
    },

    windowOutToBottom:function (node) {
        node.active = true
        node.x = 0
        let seq = cc.sequence(cc.moveTo(0.12,0,-cc.winSize.height/2),
            cc.callFunc(function () {
                this.active = false
            }.bind(node),this))
        node.runAction(seq)
    },

    //游客状态时，访问敏感内容，需要弹出对话框提示其登录
    showRegisterScene:function (cb) {
        cb()
        return

        //测试用
        if(cc.gg.gameDataUtil.guestCanOpenEverything && cb){
            cb()
            return
        }

        if(GameDataUtilTs.LoginMethod.GUEST === GameDataUtilTs.getLoginType()){
            let a = LanguageMngTs.getSentence(3)    //Notice
            let b = LanguageMngTs.getSentence(57)   //Yes
            let c = LanguageMngTs.getSentence(179)  //Cancel
            let d = LanguageMngTs.getSentence(230)  //You must register
            cc.gg.toastMgr.showDialog(a,b,c,d,
                function () {
                    cc.gg.gameDataUtil.goToLogin(true)
                }.bind(this),
                function () {
                    cc.gg.toastMgr.removeDialog()
                }.bind(this)
            )
        }else if(cb){
            cb()
        }
    },

    accountReplaced:function () {
        let title = LanguageMng.getSentence(3)      //Notice
        let btnStr = LanguageMng.getSentence(181)   //ok
        let contentStr = LanguageMng.getSentence(232)   //Your account has been logged in elsewhere
        let cb = function () {
            cc.gg.gameDataUtil.goToLogin(true)
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
    },

    getCurrentSceneName:function () {
        let sceneName = cc.director.getScene().name
        return sceneName
    },

    onKickSinceCoinNotEnough(){
        let sceneName = this.getCurrentSceneName()
        if(sceneName === LogicConfigTs.SCENENAME_GAME_2 || sceneName === LogicConfigTs.SCENENAME_GAME_6){
            GameDataUtilTs.isKickCauseNoMoney = true
            cc.gg.gameDataUtil.goToLobby(true)
        }else if(sceneName === LogicConfigTs.SCENENAME_LOBBY){
            this.showKickByGold()
        }
    },

    showKickByGold:function () {
        let title = LanguageMng.getSentence(3) //Notice
        let btnStr1 = LanguageMng.getSentence(57)   //Yes
        let btnStr2 = LanguageMng.getSentence(56)   //No
        let contentStr = "You quit the room because there's not enough money. Do you need to recharge?"
        let cb1 = function () {
            let kv = {
                keys:["NoMoneyGoWallet"],
                values:["1"]
            }
            let jsonStr = JSON.stringify(kv)
            cc.gg.tdUtil.doSendTdEvent("click_game_no_money", "wallet", jsonStr);
            cc.gg.toastMgr.removeDialog()
                cc.gg.logicUtil.showRegisterScene(function () {
                    cc.loader.loadRes("windows/Wallet001",cc.Prefab,function (err, prefab) {
                        cc.gg.toastMgr.removeNetWaitting()
                        this._loadedWallet = true
                        let newNode = cc.instantiate(prefab)
                        cc.find("Canvas/Windows").addChild(newNode)
                        newNode.getComponent("Wallet").initWallet(this)
                        cc.loader.releaseRes("windows/Wallet001")
                    }.bind(this))
                }.bind(this))
        }.bind(this)
        let cb2 = function () {
            cc.gg.toastMgr.removeDialog()
        }.bind(this)
        cc.gg.toastMgr.showDialog(title,btnStr1 ,btnStr2 ,contentStr,cb1,cb2) 
    },

    showKickByReplace:function () {
        let title = LanguageMng.getSentence(3) //Notice
        let btnStr = LanguageMng.getSentence(181)   //ok
        let contentStr = LanguageMngTs.getSentence(205) //Your account has logged on other device.
        let cb = function () {
            cc.gg.toastMgr.removeDialog()
            cc.gg.gameDataUtil.goToLogin()
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
    },
});
